# coding = utf-8

from PySide6.QtWidgets import (QDialog,QLabel,QLineEdit,QCheckBox,QGroupBox,QRadioButton,
                               QPushButton,QVBoxLayout,QHBoxLayout,QPlainTextEdit,QMessageBox)
from PySide6.QtCore import QRegularExpression
from PySide6.QtGui import QTextDocument,QTextCursor
import json


class QFindTextDialog(QDialog):

    def __init__(self,plainTextEdit: QPlainTextEdit,upDownLablel:QLabel, parent=None):
        """初始化
        """
        self.plainTextEdit = plainTextEdit
        self.upDownLablel = upDownLablel
        super().__init__(parent)
        self.setupUi()
        self.setEvnetBind()
    
    def setupUi(self):
        self.setWindowTitle("查找")

        self.findLabel = QLabel("查找内容(&N)")  # 需要设置伙伴关系
        self.findLineEdit = QLineEdit()
        self.findLabel.setBuddy(self.findLineEdit)

        # 全局布局 使用垂直布局
        gVboxLayout = QVBoxLayout(self)


        hboxLayout = QHBoxLayout()
        hboxLayout.addWidget(self.findLabel)
        hboxLayout.addWidget(self.findLineEdit)

        # 区分大小写 与循环
        self.caseSensitiveCheck = QCheckBox("区分大小写")
        self.cycleCheck = QCheckBox("循环")
        self.wordCheck = QCheckBox("匹配完整单词")
        
        vboxLayout = QVBoxLayout()
        vboxLayout.addWidget(self.caseSensitiveCheck)
        vboxLayout.addWidget(self.cycleCheck)
        vboxLayout.addWidget(self.wordCheck)

        # 方向 组合盒子 
        self.directGroupBox = QGroupBox()
        self.directGroupBox.setTitle("方向")
        self.upButton = QRadioButton("向上(&U)")
        self.downButton = QRadioButton("向下(&D)")
        directVLayout = QVBoxLayout(self.directGroupBox)
        directVLayout.addWidget(self.upButton)
        directVLayout.addWidget(self.downButton)
        
        # 查找下一个与取消按钮
        self.findNextButton = QPushButton("查找下一个(F)")
        self.cancelButton = QPushButton("取消")
        vboxLayout_ = QVBoxLayout()
        vboxLayout_.addWidget(self.findNextButton)
        vboxLayout_.addWidget(self.cancelButton)

        # 添加部件与控件
        gVboxLayout.addLayout(hboxLayout)
        gVboxLayout.addLayout(vboxLayout)
        gVboxLayout.addWidget(self.directGroupBox)
        gVboxLayout.addLayout(vboxLayout_)

        self.setStat() #放到构造函数会一直读取

    def setEvnetBind(self):
        """设置事件绑定
        """
        # 查找下一个文本文档
        self.findNextButton.clicked.connect(self.setTextCursor)

        # 取消按钮触发关闭
        self.cancelButton.clicked.connect(self.close)
      
        self.cancelButton.clicked.connect(self.saveStatusTofile)

    def getRadioBtnStatus(self) -> tuple[bool,bool]:
        """获取圆角按钮状体
        :return: 俩个按钮状态布尔值 的元组
        """
        return (self.upButton.isChecked(),self.downButton.isChecked())
    
    def getCheckBoxStatus(self) -> tuple[bool,bool]:
        """获取复选框的状态

        :return: 表示复选框状态的元组值
        """
        return (self.caseSensitiveCheck.isChecked(),self.cycleCheck.isChecked(),self.wordCheck.isChecked)

    def getCycleCheckBoxStatus(self) -> bool:
        """获取循环复选框状态

        :return: True|False
        """
        return self.cycleCheck.isChecked()

    def getFindFlags(self):
        """设置查找标签
            循环未实现
            后续添加完整单词
        """
        # 上一个标签
        prevFlags = QTextDocument.FindFlag.FindBackward 
        # 区分大小写标签
        caseFlags=  QTextDocument.FindFlag.FindCaseSensitively
        # 匹配完整单词标签
        wordFlags = QTextDocument.FindFlag.FindWholeWords

        # 获取圆角按钮状态
        upBtnStat,downBtnStat = self.getRadioBtnStatus()
        # 获取复选框状态
        caseSensitiveStat,cycleStat,wordStat = self.getCheckBoxStatus()

        if upBtnStat:
            flags = prevFlags
            if caseSensitiveStat and wordStat:
                flags = prevFlags | caseFlags | wordFlags
            elif caseSensitiveStat:
                flags = prevFlags | caseFlags
            elif wordStat:
                flags = prevFlags | wordFlags
        elif downBtnStat:
            flags = None
            if caseSensitiveStat and wordStat:
                flags = caseFlags | wordFlags
            elif caseSensitiveStat:
                flags = caseFlags
            elif wordStat:
                flags = wordFlags
        return flags

    def setTextCursor(self):
        """设置文本光标
        """
        findStr = self.findLineEdit.text()
        flags = self.getFindFlags()
        if flags:
            findRes = self.plainTextEdit.find(findStr,flags)
            self.isResetCursor(findRes)
        else:
            findRes = self.plainTextEdit.find(findStr)
            self.isResetCursor(findRes)

    def setNoFindMsgBox(self,findResult:bool):
        """弹出找不到提示
        """
        if findResult == False:
            findStr = self.findLineEdit.text()
            # 提示窗口
            msgBox = QMessageBox(self)
            msgBox.setWindowTitle("记事本") #查找窗口太小 时不会显示
            msgBox.setIcon(QMessageBox.Icon.Information)
            msgBox.setText(f'"{findStr}"')
            msgBox.exec()

    def isResetCursor(self,findResult:bool):
        """重新设置光标
        """
        cycleStatus = self.getCycleCheckBoxStatus()
        prevBtnStat,nextBtnStat = self.getRadioBtnStatus()
        # 如果勾选 按钮为向上 从最后开始查找
        # 如果勾选 按钮为向下 从开头开始查找
        if cycleStatus:
            if prevBtnStat and findResult == False:
                self.updateStatusBar("从底部开始查找下一项")
                self.plainTextEdit.moveCursor(QTextCursor.MoveOperation.End)
            elif nextBtnStat and findResult == False:
                self.updateStatusBar("从顶部开始查找下一项")
                self.plainTextEdit.moveCursor(QTextCursor.MoveOperation.Start)
            else:
                self.updateStatusBar(""*10)
        else:
            if findResult == False:
                self.setNoFindMsgBox(findResult)

    def getStatWithFile(self) -> tuple[str,bool,bool,bool,bool,bool]:
        """从文件获取状态 

        :return: 查找字符串,忽略大小写状态，循环状态，单词状态，上一个状态
        """
        with open("cache/Dialog/findTextDialog/config.json","r",encoding="utf-8") as file:
            data = json.load(file)
            tupData =  (data["findStr"],data["caseCheck"],data["cycleCheck"],data["wordCheck"],data["upBtn"],data["downBtn"])
            return tupData
            
    def setStat(self):
        """设置状态
        """
        findStr,caseStat,cycleStat,workStat,upStat,downBtn = self.getStatWithFile()
        self.findLineEdit.setText(findStr)
        self.caseSensitiveCheck.setChecked(caseStat)
        self.cycleCheck.setChecked(cycleStat)
        self.wordCheck.setChecked(workStat)
        self.upButton.setChecked(upStat)
        self.downButton.setChecked(downBtn)

    def saveStatusTofile(self):
        """ 保存状态到文件
        """
        findStr = self.findLineEdit.text()
        caseStat = self.caseSensitiveCheck.isChecked()
        cycyleStat = self.cycleCheck.isChecked()
        wordStat = self.wordCheck.isChecked()
        upBtn = self.upButton.isChecked()
        downBtn = self.downButton.isChecked()

        data = {
            "findStr":findStr,
            "caseCheck":caseStat,
            "cycleCheck": cycyleStat,
            "wordCheck": wordStat,
            "upBtn": upBtn,
            "downBtn": downBtn
        }

        jsonData = json.dumps(data, ensure_ascii=False, indent=4)

        with open("cache/Dialog/findTextDialog/config.json","w",encoding="utf-8") as file:
            file.write(jsonData)

    def updateStatusBar(self,text:str):
        """更新状态栏文本
        """
        # self.upDownLablel.setParent(self)
        self.upDownLablel.setText(text)
